package com.crazypos.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.CategoryEntity;
import com.crazypos.pojo.bo.CreateCategoryBO;
import com.crazypos.pojo.bo.UpdateCategoryBO;
import com.crazypos.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getCategoryList(Integer startPage, Integer pageSize, String keyword, Integer isSystem) {
        IPage<CategoryEntity> result = categoryService.adminGetCategoryList(startPage, pageSize, keyword, isSystem);
        return ApiRestResponse.success(result);
    }

    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createCategory(@RequestBody @Validated CreateCategoryBO bo) {
        String currentId = SecurityUtil.getUserId();
        categoryService.createCategory(true, bo.getName(), "", currentId);
        return ApiRestResponse.success();
    }

    @PostMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse updateCategory(@RequestBody @Validated UpdateCategoryBO bo) {
        categoryService.updateCategory(true, bo.getId(), bo.getName(), "");
        return ApiRestResponse.success();
    }

}
